package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.ManualAcctInfoMapper;
import cc.rengu.igas.bomp.common.entity.ManualAcctInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 手工挂账信息表操作接口实现类
 * Created by 王成 on 2020/3/21
 */
public class ManualAcctInfoMapperImpl implements ManualAcctInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertManualAcctInfo(ManualAcctInfo manualAcctInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertManualAcctInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, manualAcctInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记手工挂账信息失败,instId:<{}>,id:<{}>,payerAcctInfo:<{}>,payeeAcctInfo:<{}>,adjustAmt:<{}>,iReturnCode:<{}>",
                    manualAcctInfo.getInstId(), manualAcctInfo.getId(), manualAcctInfo.getPayerAcctInfo(), manualAcctInfo.getPayeeAcctInfo(), manualAcctInfo.getAdjustAmt(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateManualAcctInfoById(ManualAcctInfo manualAcctInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateManualAcctInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, manualAcctInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新手工挂账信息失败,instId:<{}>,id:<{}>,payerAcctInfo:<{}>,payeeAcctInfo:<{}>,adjustAmt:<{}>,iReturnCode:<{}>",
                    manualAcctInfo.getInstId(), manualAcctInfo.getId(), manualAcctInfo.getPayerAcctInfo(), manualAcctInfo.getPayeeAcctInfo(), manualAcctInfo.getAdjustAmt(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public ManualAcctInfo selectManualAcctInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectManualAcctInfoById");
        ManualAcctInfo manualAcctInfo = new ManualAcctInfo();
        manualAcctInfo.setInstId(instId);
        manualAcctInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, manualAcctInfo, manualAcctInfo);
        if (0 != iReturnCode) {
            rglog.error("获取手工挂账信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return manualAcctInfo;
    }

    @Override
    public List<ManualAcctInfo> selectManualAcctInfoByAdjustTime(String instId, String startDate, String endDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectManualAcctInfoByAdjustTime");
        ManualAcctInfo manualAcctInfo = new ManualAcctInfo();
        manualAcctInfo.setInstId(instId);
        manualAcctInfo.setAdjustDate(startDate);
        manualAcctInfo.setRemark1(endDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, manualAcctInfo, ManualAcctInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取手工挂账信息失败,instId:<{}>,startDate:<{}>,endDate:<{}>", instId, startDate, endDate);
            return null;
        }
        return objectList.stream().map(item -> (ManualAcctInfo) item).collect(Collectors.toList());
    }
}
